package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"huxiaopang/models"
	"strings"
	"fmt"
)

type BindController struct {
	ApiBaseController
}

// @router /auth [post]
func (this *BindController) Auth() {
	bean := BaseBean{}
	devId := this.GetString("dev_id")
	if len(devId) == 0 {
		bean.Code = "-1"
		bean.Message = "dev_id不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	deviceData := models.DeviceData{}
	deviceData.DeviceId = devId

	if err := deviceData.Read("DeviceId"); err != nil && err!=orm.ErrNoRows{
		bean.Code = "-1"
		bean.Message = "系统繁忙"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}else if err == orm.ErrNoRows{
		beego.Debug("未绑定门店")
		bean.Code = "-1"
		bean.Message = "未绑定门店"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}else {
		var deviceAuthBean DeviceAuthBean
		deviceAuthBean.RegId = deviceData.RegId
		deviceAuthBean.StoreCode = deviceData.StoreCode
		deviceAuthBean.DeviceId = deviceData.DeviceId
		this.Data["json"] = map[string]interface{}{
			"code": "0",
			"msg":  "SUCCESS",
			"data": deviceAuthBean,
		}
		this.ServeJSON()
		return
	}
}


// @router /active [post]
func (this *BindController) Active() {
	bean := BaseBean{}
	devId := this.GetString("dev_id")
	storeCode := this.GetString("store_code")
	regId := this.GetString("reg_id")


	if len(devId) == 0 {
		bean.Code = "-1"
		bean.Message = "dev_id不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	if len(storeCode) == 0 {
		bean.Code = "-1"
		bean.Message = "store_code不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	if len(regId) == 0 {
		bean.Code = "-1"
		bean.Message = "reg_id不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	deviceData := models.DeviceData{}
	deviceData.DeviceId = devId

	if err := deviceData.Read("DeviceId");  err != nil && err!=orm.ErrNoRows{
		bean.Code = "-1"
		bean.Message = "系统繁忙"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}else if err == orm.ErrNoRows{
		deviceData.StoreCode = storeCode
		deviceData.RegId = regId
		deviceData.Insert()
	}else {
		bean.Code = "-1"
		bean.Message = "已绑定门店"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	var deviceAuthBean DeviceAuthBean
	deviceAuthBean.DeviceId = devId
	deviceAuthBean.StoreCode = storeCode
	deviceAuthBean.RegId = regId

	this.Data["json"] = map[string]interface{}{
		"code": "0",
		"msg":  "SUCCESS",
		"data": deviceAuthBean,
	}
	this.ServeJSON()
	return
}



// @router /bindtablecode [post]
func (this *BindController) BindTableCode() {
	bean := BaseBean{}
	devId := this.GetString("dev_id")
	uuid := strings.Replace(this.GetString("uuid"), "\n", "", -1)
	beego.Debug("devId:" + devId + "\n" + "uuid:" + uuid)
	var storeCode string
	var tableCode string

	if len(devId) == 0 {
		bean.Code = "-1"
		bean.Message = "dev_id不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	if len(uuid) == 0 {
		bean.Code = "-1"
		bean.Message = "uuid不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	tableNumberData := models.TableNumberData{}
	tableNumberData.Uuid = uuid
	if err := tableNumberData.Read("Uuid"); err != nil && err!=orm.ErrNoRows{
		bean.Code = "-1"
		bean.Message = "系统繁忙"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}else if err == orm.ErrNoRows{
		bean.Code = "-1"
		bean.Message = "未查询到该uuid"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}else {
		storeCode = tableNumberData.StoreCode
		tableCode = tableNumberData.TableCode
	}
	beego.Debug("storeCode:" + storeCode + " tableCode:" + tableCode)

	deviceData := models.DeviceData{}
	deviceData.DeviceId = devId
	deviceData.StoreCode = storeCode
	beego.Debug("DeviceId:" + deviceData.DeviceId + " StoreCode:" + deviceData.StoreCode)
	if err := deviceData.Read("DeviceId", "StoreCode");  err != nil && err!=orm.ErrNoRows{
		bean.Code = "-1"
		bean.Message = "系统繁忙"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}else if err == orm.ErrNoRows{
		bean.Code = "-1"
		bean.Message = "请先绑定门店"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}else {
		//查询device_tablecode_data表
		deviceTableCodeData := models.DeviceTableCodeData{}
		deviceTableCodeData.StoreCode = storeCode
		deviceTableCodeData.TableCode = tableCode
		if err := deviceTableCodeData.Read("StoreCode", "TableCode");  err != nil && err!=orm.ErrNoRows{
			bean.Code = "-1"
			bean.Message = "系统繁忙"
			this.Data["json"] = bean
			this.ServeJSON()
			return
		}else if err == orm.ErrNoRows{
			//插入
			deviceTableCodeData.DeviceId = devId
			if err := deviceTableCodeData.Insert(); err != nil{
				bean.Code = "-1"
				bean.Message = "绑定失败"
				this.Data["json"] = bean
				this.ServeJSON()
				return
			}else {
				var deviceBindBean DeviceBindBean
				deviceBindBean.StoreCode = storeCode
				deviceBindBean.TableCode = tableCode
				deviceBindBean.DeviceId = devId
				this.Data["json"] = map[string]interface{}{
					"code": "0",
					"msg":  "绑定成功",
					"data": deviceBindBean,
				}
				this.ServeJSON()
				return
			}
		}else {
			bean.Code = "-1"
			bean.Message = "该门店该桌码已被绑定"
			this.Data["json"] = bean
			this.ServeJSON()
			return
		}
	}

}

// @router /getbindtablecodeList [post]
func (this *BindController) GetBindTableCodeList() {
	bean := BaseBean{}
	devId := this.GetString("dev_id")
	beego.Debug("devId:" + devId)
	if len(devId) == 0 {
		bean.Code = "-1"
		bean.Message = "dev_id不能为空"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}

	var list []string
	var tableCodeListsSql string
	o := orm.NewOrm()
	tableCodeListsSql = fmt.Sprintf("SELECT table_code from device_tablecode_data WHERE device_id = \"%s\" ", devId)
	beego.Debug(tableCodeListsSql)
	tableCodeListsNum, tableCodeListsErr := o.Raw(tableCodeListsSql).QueryRows(&list)
	if tableCodeListsErr != nil || tableCodeListsNum < 0 {
		bean.Code = "-1"
		bean.Message = "查询失败"
		this.Data["json"] = bean
		this.ServeJSON()
		return
	}
	beego.Debug(list)
	this.Data["json"] = map[string]interface{}{
		"code": "0",
		"msg":  "SUCCESS",
		"data": list,
	}
	this.ServeJSON()
	return
}

